<?php
namespace app\api\logic;
use think\Db;
class Appointment extends Base{

    public function __construct()
    {
        parent::__construct();
    }

    public function apply($param)
    {
        $sid = isset($param['sid'])?intval($param['sid']) : 0;
        $baby_name = isset($param['baby_name'])?trim($param['baby_name']) : '';
        $age = isset($param['age'])?trim($param['age']) : '';
        $tel = isset($param['tel'])?trim($param['tel']) : '';
//        $province = isset($param['province'])?trim($param['province']) : '';
 //       $city = isset($param['city'])?trim($param['city']) : '';
  //      $area = isset($param['area'])?trim($param['area']) : '';
   //     $address = isset($param['address'])?trim($param['address']) : '';
        $content = isset($param['content'])?trim($param['content']) : '';
        $tid = isset($param['tid'])?intval($param['tid']) : 0;
        if(!$sid || !$baby_name|| !$age|| !$tel){
            $this->return_data['Msg'] = '缺少参数';
            return $this->return_data;
        }
        $ap =Db::name('appointment')->where([['tel','=',$tel],['sid','=',$sid]])->find();
        if ($ap) {
            $this->return_data['Msg'] = '已预约,请勿重复预约';
            return $this->return_data;
        }
        $teacher = '';
        if($tid){
          $teacher =Db::name('teacher')->where([['id','=',$tid],['sid','=',$sid]])->find();
          if (!$teacher) {
              $this->return_data['Msg'] = '无此老师';
              return $this->return_data;
          }
        }
        $adata['sid'] = $sid;
        $adata['baby_name'] = $baby_name;
        $adata['age'] = $age;
        $adata['tel'] = $tel;
        $adata['tid'] = $tid;
        if($teacher)
          $adata['teacher_name'] = $teacher['name'];
       // $adata['province'] = $province;
      //  $adata['city'] = $city;
     //   $adata['area'] = $area;
    //    $adata['address'] = $address;
        $adata['content'] = $content;
        $adata['createtime'] = time();
        $adata['updatetime'] = time();
        $res=Db::name('appointment')->insertGetId($adata);
        if (!$res) {
            $this->return_data['Msg'] = '添加预约信息失败';
            return $this->return_data;
        }
        $apdata['sid'] = $sid;
        $apdata['phone'] =  substr_replace($tel, 'xxxx',3, 4);
        $apdata['content'] =  $baby_name.'预约了课程';
        Db::name('appointing')->insertGetId($apdata);
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = '预约成功';
        $this->return_data['Data'] = $adata;
        return $this->return_data;
    }


}
